package helpers;

import java.util.concurrent.ConcurrentHashMap;

public class DistanceCalculator {
	
	// Calculate distance from a given RSSI value, using a linear regression model (Least squares fitting)
	
	private String reader;
	private Float slope;
	private Float intercept;
	private Float x;
	private Float y;
	
	private static ConcurrentHashMap<String, DistanceCalculator> calcs;
	private static boolean initialized = false;
	
	public static ConcurrentHashMap<String, DistanceCalculator> getCalcs() {
		if (!initialized)
			initialize();
		return calcs;
	}

	private static void initialize() {
		calcs = new ConcurrentHashMap<>();
		
		// Initialize with regression lines for each reader

        //y = 0.878x + 28.227      old
        //y = -0.9625x + 30
		calcs.put("1346", new DistanceCalculator("1346", -0.9625f, 30.227f,0f, 0f));
		calcs.put("1178", new DistanceCalculator("1178", 0.6f*-0.9625f, 0.6f*30.227f,12.6f,16.2f));
		calcs.put("1169", new DistanceCalculator("1169", -0.9625f, 30.227f, 24f, 0f));
        calcs.put("1101", new DistanceCalculator("1169", 0.5f*-0.9625f, 0.5f*30.227f, 11.4f, 4.8f));


        initialized = true;
		
	}

	public DistanceCalculator(String reader, Float slope, Float intercept, Float x, Float y){
		this.reader = reader;
		this.slope = slope;
		this.intercept = intercept;
		this.x = x;
		this.y = y;
	}
	
	public String getReader() {
		return reader;
	}
	public void setReader(String reader) {
		this.reader = reader;
	}
	public Float getSlope() {
		return slope;
	}
	public void setSlope(Float slope) {
		this.slope = slope;
	}
	public Float getIntercept() {
		return intercept;
	}
	public void setIntercept(Float intercept) {
		this.intercept = intercept;
	}
	
	public Float getX() {
		return x;
	}

	public Float getY() {
		return y;
	}
	
	public Float calcDistance(Float rssi){
		// y = ax + b , 'y' is distance 'x' is rssi 'a' is slope 'b' is intercept
		return (slope * rssi + intercept);
	}
	

}
